package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.linlinjava.litemall.db.dao.LitemallFormSupplyMapper;
import org.linlinjava.litemall.db.dao.SupplyMapper;
import org.linlinjava.litemall.db.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SupplyService {
    @Resource
    private SupplyMapper supplyMapper;
    @Resource
    private LitemallFormSupplyMapper formSupplyMapper;
    @Autowired
    private LitemallUserService userService;

    public List<Map> getSupplys(String query,String orderByClause) {
        return supplyMapper.selectSupplys(query, orderByClause);
    }

    public LitemallFormSupply query(Integer userId, Integer id) {
    	LitemallFormSupplyExample example = new LitemallFormSupplyExample();
        example.or().andIdEqualTo(id).andUserIdEqualTo(userId).andDeletedEqualTo(false);
        return formSupplyMapper.selectOneByExample(example);
    }

    public int add(LitemallFormSupply formSupply) {
    	formSupply.setAddTime(LocalDateTime.now());
    	formSupply.setUpdateTime(LocalDateTime.now());
        return formSupplyMapper.insertSelective(formSupply);
    }

    public int update(LitemallFormSupply formSupply) {
    	formSupply.setUpdateTime(LocalDateTime.now());
        return formSupplyMapper.updateByPrimaryKeySelective(formSupply);
    }

    public Object delete(Integer userId,Integer id) {
        LitemallFormSupplyExample example = new LitemallFormSupplyExample();
        LitemallFormSupplyExample.Criteria criteria = example.createCriteria();

        example.or().andIdEqualTo(id).andDeletedEqualTo(false);
        LitemallFormSupply wantBuy = formSupplyMapper.selectOneByExample(example);
        LitemallUser user = userService.findById(userId);
        if(user.getShopId()!=wantBuy.getShopId()){
            return null;
        }
        wantBuy.setStatus((byte) 3);
        return formSupplyMapper.updateByExampleSelective(wantBuy, example);
    }
    
}
